package cn.com.aisat.android.videoshow.db;

import java.io.InputStream;
import java.util.ArrayList;

public class PullEarthquakeHandler {
/*	//xml解析用到的Tag
	private String kEntryElementName = "entry";
	private String kLinkElementName = "link";
	private String kTitleElementName = "title";
	private String kUpdatedElementName = "updated";
	private String kGeoRSSPointElementName = "georss:point";
	private String kGeoRSSElevElementName = "georss:elev";
	//用于保存xml解析获取的结果
	private ArrayList<EarthquakeEntry> earthquakeEntryList = null;
	private EarthquakeEntry earthquakeEntry = null;	
	private Boolean startEntryElementFlag = false;
	//解析xml数据
	public ArrayList<EarthquakeEntry> parse(InputStream inStream)
	{
		try {
						//创建XmlPullParser,有两种方式
			//方式一:使用工厂类XmlPullParserFactory
			XmlPullParserFactory pullFactory = XmlPullParserFactory.newInstance();
			XmlPullParser xmlPullParser = pullFactory.newPullParser();
//			//方式二:使用Android提供的实用工具类android.util.Xml
//			XmlPullParser xmlPullParser = Xml.newPullParser();
			xmlPullParser.setInput(inStream, "UTF-8");
			int eventType = xmlPullParser.getEventType();
			boolean isDone = false;	
			//具体解析xml
			while ((eventType != XmlPullParser.END_DOCUMENT)&&(isDone != true)) {
				String localName = null;
				switch (eventType) {
					case XmlPullParser.START_DOCUMENT:
					{
						earthquakeEntryList = new ArrayList<EarthquakeEntry>();
					}
						break;
					case XmlPullParser.START_TAG:
					{
						localName = xmlPullParser.getName();
						if(localName.equalsIgnoreCase(kEntryElementName))
						{
							earthquakeEntry = new EarthquakeEntry();
							startEntryElementFlag = true;
						}
						else if(startEntryElementFlag == true)
						{
							String currentData = null;
							if(localName.equalsIgnoreCase(kTitleElementName))
							{
								currentData = xmlPullParser.nextText();
//								Log.v("Pull", currentData);
							    //提取强度信息
							    String magnitudeString = currentData.split(" ")[1];
							    int end =  magnitudeString.length()-1;
							    magnitudeString = magnitudeString.substring(0, end);
							    double magnitude = Double.parseDouble(magnitudeString);
							    earthquakeEntry.setMagnitude(magnitude);
							    //提取位置信息
							    String place = currentData.split(",")[1].trim();
							    earthquakeEntry.setPlace(place);								
							}
							else if (localName.equalsIgnoreCase(kUpdatedElementName)) {
								currentData = xmlPullParser.nextText();
//								Log.v("Pull", currentData);
								//构造更新时间
								SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
								Date qdate = new GregorianCalendar(0,0,0).getTime();
						        try {
							      qdate = sdf.parse(currentData);
							    } catch (ParseException e) {
							      e.printStackTrace();
							    }
							    earthquakeEntry.setDate(qdate);
							}
							else if (localName.equalsIgnoreCase(kGeoRSSPointElementName)) {
								currentData = xmlPullParser.nextText();
//								Log.v("Pull", currentData);
								//提取经纬度信息
							    String[] latLongitude = currentData.split(" ");
							    Location location = new Location("dummyGPS");
							    location.setLatitude(Double.parseDouble(latLongitude[0]));
							    location.setLongitude(Double.parseDouble(latLongitude[1]));
							    earthquakeEntry.setLocation(location);
							}
							else if (localName.equalsIgnoreCase(kGeoRSSElevElementName)) {
								currentData = xmlPullParser.nextText();
//								Log.v("Pull", currentData);
							    //提取海拔高度信息
								double evel;
								//因为USGS数据有可能会输错，比如为"--10000"，多了一个"-"号
							    try {
							    	evel = Double.parseDouble(currentData);
								} catch (Exception e) {
									// TODO: handle exception
									e.printStackTrace();
									evel = 0;
								}
							    Log.v("Pull_Elev", String.valueOf(evel));
							    earthquakeEntry.setElev(evel);
							}
							else if(localName.equalsIgnoreCase(kLinkElementName))
							{
								String webLink = xmlPullParser.getAttributeValue("", "href");
								earthquakeEntry.setLink(webLink);
//								Log.v("Pull", webLink);
							}
						}					
					}
						break;
					case XmlPullParser.END_TAG:
					{
						localName = xmlPullParser.getName();
						if((localName.equalsIgnoreCase(kEntryElementName))&&(startEntryElementFlag==true))
						{
							earthquakeEntryList.add(earthquakeEntry);
							startEntryElementFlag = false;
						}
					}
						break;
					default:
						break;
					}
				eventType = xmlPullParser.next();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Log.v("Pull", "End");
		return earthquakeEntryList;
	}*/
}
